package Operation;

import Book.BookList;
import Book.Book;

import java.util.Scanner;

public class AddBook implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书.....");
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要添加的书名：");
        String bookName = sc.nextLine();
        System.out.println("请输入要添加的作者：");
        String author = sc.nextLine();
        System.out.println("请输入要添加的类型：");
        String type = sc.nextLine();
        System.out.println("请输入要添加的价格：");
        int price = sc.nextInt();
        Book book = new Book(bookName,author,price,type);

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if(tmp.getName().equals(bookName)) {
                System.out.println("有这本书，可以不上架！！");
                return;
            }
        }

        bookList.setBooks(book,currentSize);

        bookList.setUsedSize(currentSize+1);


        //3.没有这本书 则放到书籍数组当中
        //看空间是否满
//        if(bookList.getUsedSize() < bookList.getCapacity()) {
//            //没满则直接存放
//            bookList.setBooks(bookList.getUsedSize(),book);
//
//            bookList.setUsedSize(bookList.getUsedSize()+1);
//            System.out.println("上架成功！");
//        }else{
//            //满了则扩容后存放
//            int newcapacity = bookList.getCapacity()*2;
//            Book[] newbooks = new Book[newcapacity];
//            BookList newbookList = new BookList(newbooks);
//            newbookList.setCapacity(newcapacity);
//            newbookList.setUsedSize(bookList.getUsedSize());
//            //拷贝原书架
//            for (int i = 0; i < bookList.getUsedSize(); i++) {
//                Book tmp = bookList.getBook(i);
//                newbookList.setBooks(i,tmp);
//            }
//            bookList = newbookList;
//            newbookList = null;
//
//            bookList.setBooks(bookList.getUsedSize(),book);
//            bookList.setUsedSize(bookList.getUsedSize()+1);
//            System.out.println("上架成功！");
//        }
//    }
//

    }



    private boolean isFull(BookList bookList) {
        return bookList.getBooks().length ==
                bookList.getUsedSize();
    }

}
